package com.lagou.demo.dao;

import com.lagou.demo.pojo.User;
import com.lagou.demo.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginDaoImp implements LoginDao {
    @Override
    public boolean login(User user) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet =null;
        try {
            connection = DruidUtil.getConnection();
            String sql="select * from t_user where username=? and password =?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2,user.getPassword());
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                User user1 = new User();
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                user1.setUserName(username);
                user1.setPassword(password);
                if (user1!=null){
                    return true;
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection,preparedStatement,resultSet);
        }
        return false;
    }
}
